package cn.edu.scau.cmi.tangqingning.comprehensive.view;

import java.io.IOException;
import java.net.URL;

import cn.edu.scau.cmi.tangqingning.abstractFactoryMethod.AbstractFactoryController;
import cn.edu.scau.cmi.tangqingning.factoryMethod.FactoryController;
import cn.edu.scau.cmi.tangqingning.simpleFactoryMethod.SimpleFactoryController;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.JavaFXBuilderFactory;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.stage.Stage;

public class ComprehensiveController {

	private Parent root;
	private Stage stage;

	@FXML
	private Label lblNum;
	@FXML
	private Label lblName;
	@FXML
	private Button btnSingle;
	@FXML
	private Button btnMultiply;
	@FXML
	private Button btnSimpleFactory;
	@FXML
	private Button btnAbstractFactory;
	@FXML
	private Button btnFactory;
	@FXML
	private Button btnObjectAdapter;
	@FXML
	private Button btnClassAdapter;
	@FXML
	private Button btnSafe;
	@FXML
	private Button btnConsistent;

	 @FXML
    private Button btnHibernate;

	 @FXML
    private Button btnSpring;

	@FXML
	private void single(ActionEvent event){
		try {
			root = FXMLLoader.load(getClass().getResource("../../simpleSingleton/simpleSingleton.fxml"));
			stage = new Stage();
			stage.setScene(new Scene(root));
			stage.setTitle("单例模式");
			stage.show();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	@FXML
	private void multiply(ActionEvent event){
		try {
			root = FXMLLoader.load(getClass().getResource("../../multiton/multiton.fxml"));
			stage = new Stage();
			stage.setScene(new Scene(root));
			stage.setTitle("多例模式");
			stage.show();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}
	@FXML
	private void factory(ActionEvent event){
		try {
			URL location = getClass().
					getResource("../../factoryMethod/factoryMethod.fxml");
			FXMLLoader fxmlLoader = new FXMLLoader();
	        fxmlLoader.setLocation(location);
	        fxmlLoader.setBuilderFactory(new JavaFXBuilderFactory());
	        Parent root = fxmlLoader.load();
			FactoryController controller = fxmlLoader.getController();   //获取Controller的实例对象
	        //Controller中写的初始化方法
	        controller.Init();
			stage = new Stage();
			stage.setScene(new Scene(root));
			stage.setTitle("工厂模式");
			stage.show();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	@FXML
	private void simpleFactory(ActionEvent event){
		try {
			URL location2 = getClass().
					getResource("../../simpleFactoryMethod/simpleFactoryMethod.fxml");
			FXMLLoader fxmlLoader = new FXMLLoader();
	        fxmlLoader.setLocation(location2);
	        fxmlLoader.setBuilderFactory(new JavaFXBuilderFactory());
	        Parent root = fxmlLoader.load();
			SimpleFactoryController controller = fxmlLoader.getController();   //获取Controller的实例对象
	        //Controller中写的初始化方法
	        controller.Init();
			stage = new Stage();
			stage.setScene(new Scene(root));
			stage.setTitle("简单工厂模式");
			stage.show();
		} catch (IOException e) {
			// TODO: handle exception
		}
	}
	@FXML
	private void abstractFactory(ActionEvent event){
		try {
			URL location = getClass().getResource("../../abstarctFactoryMethod/abstarctFactoryMethod.fxml");
			FXMLLoader fxmlLoader = new FXMLLoader();
	        fxmlLoader.setLocation(location);
	        fxmlLoader.setBuilderFactory(new JavaFXBuilderFactory());
	        Parent root = fxmlLoader.load();
			Scene scene = new Scene(root);
			AbstractFactoryController controller = fxmlLoader.getController();   //获取Controller的实例对象
	        //Controller中写的初始化方法
	        controller.Init();
			stage = new Stage();
			stage.setScene(scene);
			stage.setTitle("抽象工厂模式");
			stage.show();
		} catch (IOException e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}
	@FXML
	private void classAdapter(ActionEvent event){
		try {
			root = FXMLLoader.load(getClass().getResource("../../adapter/classAdapter/"
					+ "classAdapter.fxml"));
			stage = new Stage();
			stage.setScene(new Scene(root));
			stage.setTitle("类设配器模式");
			stage.show();
		} catch (IOException e) {
			// TODO: handle exception
		}
	}
	@FXML
	private void objectAdapter(ActionEvent event){
		try {
			root = FXMLLoader.load(getClass().getResource("../../adapter/objectAdapter/"
					+ "objectAdapter.fxml"));
			stage = new Stage();
			stage.setScene(new Scene(root));
			stage.setTitle("对象设配器模式");
			stage.show();
		} catch (IOException e) {
			// TODO: handle exception
		}
	}
	@FXML
	private void safeComposite(ActionEvent event){
		try {
			root = FXMLLoader.load(getClass().getResource("../../safeComponent/"
					+ "safeComponent.fxml"));
			stage = new Stage();
			stage.setScene(new Scene(root));
			stage.setTitle("安全组合模式");
			stage.show();
		} catch (IOException e) {
			// TODO: handle exception
		}
	}
	@FXML
	private void consistentComposite(ActionEvent event){
		try {
			root = FXMLLoader.load(getClass().getResource("../../consistencyComponent/"
					+ "consistencyComponent.fxml"));
			stage = new Stage();
			stage.setScene(new Scene(root));
			stage.setTitle("一致性组合模式");
			stage.show();
		} catch (IOException e) {
			// TODO: handle exception
		}
	}

	@FXML
    void hibernate(ActionEvent event) {
		try {
			root = FXMLLoader.load(getClass().getResource("../../hibernate/HibernateViewer.fxml"));
			stage = new Stage();
			stage.setScene(new Scene(root));
			stage.setTitle("hibernate");
			stage.show();
		} catch (IOException e) {
			// TODO: handle exception
		}
    }

    @FXML
    void spring(ActionEvent event) {
    	try {
			root = FXMLLoader.load(getClass().getResource("../../spring/client/ClientView.fxml"));
			stage = new Stage();
			stage.setScene(new Scene(root));
			stage.setTitle("spring");
			stage.show();
		} catch (IOException e) {
			// TODO: handle exception
		}
    }

	@FXML
	void initialize(){

	}
}
